<template>
  <div class="document-flow-demo">
    <h2 class="section-title">文档流与父级塌陷</h2>
    <p class="section-intro">
      文档流是CSS布局的基础概念，理解文档流和父级塌陷问题对于掌握CSS布局至关重要。
    </p>

    <div class="demo-grid">
      <!-- 正常文档流 -->
      <div class="demo-card">
        <h3>正常文档流 (Normal Flow)</h3>
        <div class="code-example">
          <div class="code-block">
            <pre><code>/* 块级元素：独占一行，从上到下排列 */
.block {
  display: block;
  background: lightblue;
  margin: 10px 0;
  padding: 10px;
}

/* 内联元素：在同一行内从左到右排列 */
.inline {
  display: inline;
  background: lightgreen;
  padding: 5px;
}</code></pre>
          </div>
          <div class="demo-result">
            <div class="normal-flow-demo">
              <div class="block-demo">块级元素 1</div>
              <div class="block-demo">块级元素 2</div>
              <span class="inline-demo">内联元素1</span>
              <span class="inline-demo">内联元素2</span>
              <span class="inline-demo">内联元素3</span>
              <div class="block-demo">块级元素 3</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 脱离文档流 -->
      <div class="demo-card">
        <h3>脱离文档流的方式</h3>
        <div class="code-example">
          <div class="code-block">
            <pre><code>/* 1. float 浮动 */
.float-left { float: left; }

/* 2. position: absolute */
.absolute { position: absolute; }

/* 3. position: fixed */
.fixed { position: fixed; }

/* 脱离文档流的元素不占据空间 */</code></pre>
          </div>
          <div class="demo-result">
            <div class="flow-escape-demo">
              <div class="normal-box">正常元素</div>
              <div class="float-box">浮动元素</div>
              <div class="normal-box">正常元素</div>
              <div class="absolute-box">绝对定位元素</div>
              <div class="normal-box">正常元素</div>
            </div>
          </div>
        </div>
      </div>

      <!-- 父级塌陷问题 -->
      <div class="demo-card">
        <h3>父级塌陷问题</h3>
        <div class="code-example">
          <div class="code-block">
            <pre><code>/* 问题：子元素浮动导致父元素高度塌陷 */
.parent {
  border: 2px solid red;
  /* 没有设置高度 */
}

.child {
  float: left;
  width: 100px;
  height: 100px;
  background: lightblue;
}</code></pre>
          </div>
          <div class="demo-result">
            <div class="collapse-demo">
              <h4>塌陷示例：</h4>
              <div class="collapsed-parent">
                <div class="float-child">浮动子元素1</div>
                <div class="float-child">浮动子元素2</div>
              </div>
              <p class="collapse-explanation">红色边框的父元素高度塌陷了！</p>
            </div>
          </div>
        </div>
      </div>

      <!-- 清除浮动方法 -->
      <div class="demo-card">
        <h3>清除浮动的方法</h3>
        <div class="code-example">
          <div class="code-block">
            <pre><code>/* 方法1: 添加空元素清除浮动 */
.clear { clear: both; }

/* 方法2: 父元素设置 overflow */
.clearfix-overflow { overflow: hidden; }

/* 方法3: 伪元素清除浮动 (推荐) */
.clearfix::after {
  content: "";
  display: table;
  clear: both;
}

/* 方法4: 使用 Flexbox */
.flex-container { display: flex; }</code></pre>
          </div>
          <div class="demo-result">
            <div class="clearfix-demo">
              <h4>清除浮动后：</h4>
              <div class="clearfix-parent">
                <div class="float-child">浮动子元素1</div>
                <div class="float-child">浮动子元素2</div>
              </div>
              <p class="clearfix-explanation">父元素恢复正常高度！</p>
            </div>
          </div>
        </div>
      </div>

      <!-- BFC 块级格式化上下文 -->
      <div class="demo-card">
        <h3>BFC (块级格式化上下文)</h3>
        <div class="code-example">
          <div class="code-block">
            <pre><code>/* 创建BFC的方法: */
/* 1. overflow: hidden/auto/scroll */
/* 2. display: flex/grid/inline-block */
/* 3. position: absolute/fixed */
/* 4. float: left/right */

.bfc-container {
  overflow: hidden; /* 创建BFC */
  border: 2px solid blue;
}

/* BFC特性：包含浮动元素 */</code></pre>
          </div>
          <div class="demo-result">
            <div class="bfc-demo">
              <h4>BFC容器：</h4>
              <div class="bfc-container">
                <div class="float-child">BFC内的浮动元素</div>
                <div class="float-child">不会导致父级塌陷</div>
              </div>
              <div class="bfc-features">
                <strong>BFC特性：</strong>
                <ul>
                  <li>包含内部浮动元素</li>
                  <li>排斥外部浮动元素</li>
                  <li>阻止margin重叠</li>
                </ul>
              </div>
            </div>
          </div>
        </div>
      </div>

      <!-- Margin 重叠 -->
      <div class="demo-card">
        <h3>Margin 重叠问题</h3>
        <div class="code-example">
          <div class="code-block">
            <pre><code>/* 垂直margin会重叠 */
.box1 { margin-bottom: 20px; }
.box2 { margin-top: 30px; }
/* 实际间距是30px，不是50px */

/* 解决方案：使用BFC分隔 */
.bfc-wrapper {
  overflow: hidden;
}

/* 或使用padding代替margin */
.no-margin { padding: 20px 0; }</code></pre>
          </div>
          <div class="demo-result">
            <div class="margin-collapse-demo">
              <div class="margin-box1">上方盒子 (margin-bottom: 20px)</div>
              <div class="margin-box2">下方盒子 (margin-top: 30px)</div>
              <p class="margin-explanation">实际间距: 30px (较大的margin值)</p>

              <div class="bfc-separator">
                <div class="margin-box1">BFC分隔的上方盒子</div>
              </div>
              <div class="bfc-separator">
                <div class="margin-box2">BFC分隔的下方盒子</div>
              </div>
              <p class="margin-explanation">BFC分隔后间距: 50px (20px + 30px)</p>
            </div>
          </div>
        </div>
      </div>

      <!-- 实际应用场景 -->
      <div class="demo-card">
        <h3>实际应用场景</h3>
        <div class="code-example">
          <div class="code-block">
            <pre><code>/* 经典两栏布局 */
.sidebar {
  float: left;
  width: 200px;
  background: #f0f0f0;
}

.main-content {
  margin-left: 220px; /* 避开浮动元素 */
  background: white;
}

/* 现代解决方案：Flexbox */
.modern-layout {
  display: flex;
  gap: 20px;
}</code></pre>
          </div>
          <div class="demo-result">
            <div class="layout-demo">
              <h4>传统浮动布局：</h4>
              <div class="float-layout">
                <div class="sidebar-float">侧边栏</div>
                <div class="main-float">主内容区域</div>
              </div>

              <h4>现代Flex布局：</h4>
              <div class="flex-layout">
                <div class="sidebar-flex">侧边栏</div>
                <div class="main-flex">主内容区域</div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>

    <div class="flow-tips">
      <h3>文档流最佳实践</h3>
      <div class="tips-grid">
        <div class="tip-card">
          <h4>🌊 理解文档流</h4>
          <p>掌握块级元素和内联元素在文档流中的行为差异。</p>
        </div>
        <div class="tip-card">
          <h4>🔧 避免浮动布局</h4>
          <p>优先使用Flexbox或Grid，避免浮动带来的问题。</p>
        </div>
        <div class="tip-card">
          <h4>📦 善用BFC</h4>
          <p>理解BFC的特性，用于解决margin重叠和包含浮动。</p>
        </div>
        <div class="tip-card">
          <h4>🎯 语义化布局</h4>
          <p>选择合适的display值，保持HTML语义化。</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
// 这个组件主要用于展示，不需要复杂的逻辑
</script>

<style scoped lang="scss">
.document-flow-demo {
  .section-title {
    font-size: 2rem;
    color: #333;
    margin-bottom: 1rem;
    border-bottom: 3px solid #4caf50;
    padding-bottom: 0.5rem;
  }

  .section-intro {
    font-size: 1.1rem;
    color: #666;
    margin-bottom: 2rem;
    line-height: 1.6;
  }

  .demo-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(500px, 1fr));
    gap: 2rem;
    margin-bottom: 2rem;
  }

  .demo-card {
    border: 1px solid #e0e0e0;
    border-radius: 8px;
    padding: 1.5rem;
    background: #fafafa;

    h3 {
      margin: 0 0 1rem 0;
      color: #4caf50;
      font-size: 1.3rem;
    }

    .code-example {
      display: flex;
      flex-direction: column;
      gap: 1rem;
    }

    .code-block {
      background: #2d3748;
      border-radius: 6px;
      padding: 1rem;
      overflow-x: auto;

      pre {
        margin: 0;
        color: #e2e8f0;
        font-family: 'Fira Code', monospace;
        font-size: 0.9rem;
        line-height: 1.4;
      }
    }

    .demo-result {
      background: white;
      border: 1px solid #e0e0e0;
      border-radius: 6px;
      padding: 1rem;
      min-height: 100px;
    }
  }

  .flow-tips {
    .tips-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
      gap: 1rem;
    }

    .tip-card {
      background: #e8f5e8;
      border-radius: 8px;
      padding: 1.5rem;
      border-left: 4px solid #4caf50;

      h4 {
        margin: 0 0 0.5rem 0;
        color: #2e7d32;
        font-size: 1.1rem;
      }

      p {
        margin: 0;
        color: #666;
        line-height: 1.5;
      }
    }
  }
}

// 演示样式
.normal-flow-demo {
  .block-demo {
    display: block;
    background: lightblue;
    margin: 10px 0;
    padding: 10px;
    border-radius: 4px;
  }

  .inline-demo {
    display: inline;
    background: lightgreen;
    padding: 5px;
    margin: 0 2px;
    border-radius: 2px;
  }
}

.flow-escape-demo {
  position: relative;
  min-height: 200px;
  border: 1px dashed #ccc;
  padding: 10px;

  .normal-box {
    background: #e3f2fd;
    padding: 10px;
    margin: 5px 0;
    border-radius: 4px;
  }

  .float-box {
    float: left;
    background: #fff3e0;
    padding: 10px;
    margin: 5px;
    border-radius: 4px;
    width: 120px;
  }

  .absolute-box {
    position: absolute;
    top: 80px;
    right: 20px;
    background: #fce4ec;
    padding: 10px;
    border-radius: 4px;
    width: 120px;
  }
}

.collapse-demo {
  .collapsed-parent {
    border: 2px solid red;
    background: #fff;
    margin: 10px 0;
    // 注意：这里故意不清除浮动来演示塌陷
  }

  .float-child {
    float: left;
    width: 100px;
    height: 60px;
    background: lightblue;
    margin: 5px;
    padding: 10px;
    border-radius: 4px;
    font-size: 0.9rem;
  }

  .collapse-explanation {
    clear: both;
    color: #d32f2f;
    font-weight: 500;
    margin-top: 10px;
  }
}

.clearfix-demo {
  .clearfix-parent {
    border: 2px solid blue;
    background: #fff;
    margin: 10px 0;

    &::after {
      content: '';
      display: table;
      clear: both;
    }
  }

  .clearfix-explanation {
    color: #1976d2;
    font-weight: 500;
    margin-top: 10px;
  }
}

.bfc-demo {
  .bfc-container {
    overflow: hidden;
    border: 2px solid blue;
    background: #fff;
    margin: 10px 0;
  }

  .bfc-features {
    margin-top: 10px;

    ul {
      margin: 0.5rem 0;
      padding-left: 1.5rem;

      li {
        margin-bottom: 0.25rem;
        color: #666;
      }
    }
  }
}

.margin-collapse-demo {
  .margin-box1 {
    background: #ffcdd2;
    padding: 10px;
    margin-bottom: 20px;
    border-radius: 4px;
  }

  .margin-box2 {
    background: #c8e6c9;
    padding: 10px;
    margin-top: 30px;
    border-radius: 4px;
  }

  .margin-explanation {
    color: #666;
    font-size: 0.9rem;
    margin: 10px 0;
    font-style: italic;
  }

  .bfc-separator {
    overflow: hidden; // 创建BFC

    .margin-box1 {
      margin-bottom: 20px;
    }

    .margin-box2 {
      margin-top: 30px;
    }
  }
}

.layout-demo {
  h4 {
    margin: 1rem 0 0.5rem 0;
    color: #333;
  }

  .float-layout {
    border: 1px solid #ddd;
    padding: 10px;
    margin: 10px 0;

    &::after {
      content: '';
      display: table;
      clear: both;
    }

    .sidebar-float {
      float: left;
      width: 150px;
      background: #f0f0f0;
      padding: 10px;
      border-radius: 4px;
    }

    .main-float {
      margin-left: 170px;
      background: white;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
    }
  }

  .flex-layout {
    display: flex;
    gap: 20px;
    border: 1px solid #ddd;
    padding: 10px;
    margin: 10px 0;

    .sidebar-flex {
      width: 150px;
      background: #f0f0f0;
      padding: 10px;
      border-radius: 4px;
    }

    .main-flex {
      flex: 1;
      background: white;
      padding: 10px;
      border: 1px solid #ddd;
      border-radius: 4px;
    }
  }
}
</style>
